package com.gyg.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.gyg.domain.*;
import com.gyg.dao.CloudTrashMapper;
import com.gyg.returns.RespCode;
import com.gyg.returns.RespEntity;
import com.gyg.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyg.utils.TimeUtilCustomize;
import com.gyg.vo.param.TrashParam;
import com.gyg.vo.result.RecordResult;
import com.gyg.vo.result.TrashListResult;
import com.gyg.vo.result.TrashResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 文件回收表 服务实现类
 * </p>
 *
 * @author gyg
 * @since 2021-04-14
 */
@Service
public class CloudTrashServiceImpl extends ServiceImpl<CloudTrashMapper, CloudTrash> implements CloudTrashService {

    @Autowired
    private CloudPictureService cloudPictureService;

    @Autowired
    private CloudDocumentService cloudDocumentService;

    @Autowired
    private CloudAudioService cloudAudioService;

    @Autowired
    private CloudVideoService cloudVideoService;

    @Override
    public void addCloudTrash(TrashParam trashParam) {
        //当前时间 yyyy-MM-dd HH:mm:ss
        String fileTime = TimeUtilCustomize.parseDateToStr(TimeUtilCustomize.DATE_TIME_FORMAT, new Date());
        CloudTrash cloudTrash = CloudTrash.builder()
                .userId(trashParam.getUserId())
                .fileId(trashParam.getFileId())
                .fileType(trashParam.getFileType())
                .fileTime(fileTime)
                .build();

        baseMapper.insert(cloudTrash);
    }

    @Override
    public RespEntity<TrashListResult> cloudTrashList(Integer userId, Integer pageNum, Integer pageSize) {
        QueryWrapper<CloudTrash> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CloudTrash::getUserId, userId)
                .eq(CloudTrash::getDeleted, 0)
                .orderByDesc(CloudTrash::getId);

        IPage<CloudTrash> iPage = new Page<>(pageNum, pageSize);
        IPage<CloudTrash> page = super.page(iPage, queryWrapper);

        List<CloudTrash> cloudTrashList = page.getRecords();

        //文件类型分类组装数据
        List<TrashResult> trashResultList = Lists.newArrayList();
        for (CloudTrash cloudTrash : cloudTrashList) {
            TrashResult trashResult = new TrashResult();
            switch (cloudTrash.getFileType()) {
                case 1 : trashResult = getPictureRecord(cloudTrash.getFileId()); break;
                case 2 : trashResult = getDocumentRecord(cloudTrash.getFileId()); break;
                case 3 : trashResult = getAudioRecord(cloudTrash.getFileId()); break;
                case 4 : trashResult = getVideoRecord(cloudTrash.getFileId()); break;
            }
            trashResult.setFileType(cloudTrash.getFileType());
            trashResult.setFileTime(cloudTrash.getFileTime());
            trashResultList.add(trashResult);
        }

        //分页数据组装
        TrashListResult trashListResult = new TrashListResult();
        trashListResult.setPageNum(page.getCurrent());
        trashListResult.setPageSize(page.getSize());
        trashListResult.setPageCount(page.getPages());
        trashListResult.setTotal(page.getTotal());
        trashListResult.setTrashResultList(trashResultList);

        return new RespEntity<>(RespCode.SUCCESS, trashListResult);
    }

    /**
     * 图片信息组装
     * @param fileId
     * @return
     */
    private TrashResult getPictureRecord(Integer fileId) {
        CloudPicture cloudPicture = cloudPictureService.getById(fileId);
        String fileName = cloudPicture.getPictureName();
        String fileImg = cloudPicture.getPictureUrl();

        //被封禁
        if (cloudPicture.getPictureBan().equals(1)) {
            fileName = fileName + "(已被封禁)";
            fileImg = cloudPicture.getPictureImg();
        }

        TrashResult trashResult = TrashResult.builder()
                .fileId(cloudPicture.getId())
                .fileName(fileName)
                .fileImg(fileImg)
                .fileDownload(cloudPicture.getPictureDownload())
                .fileSize(cloudPicture.getPictureSize())
                .build();

        return trashResult;
    }

    /**
     * 文档信息组装
     * @param fileId
     * @return
     */
    private TrashResult getDocumentRecord(Integer fileId) {
        CloudDocument cloudDocument = cloudDocumentService.getById(fileId);
        String fileName = cloudDocument.getDocumentName();

        //被封禁
        if (cloudDocument.getDocumentBan().equals(1)) {
            fileName = fileName + "(已被封禁)";
        }

        TrashResult trashResult = TrashResult.builder()
                .fileId(cloudDocument.getId())
                .fileName(fileName)
                .fileImg(cloudDocument.getDocumentImg())
                .fileDownload(cloudDocument.getDocumentDownload())
                .fileSize(cloudDocument.getDocumentSize())
                .build();

        return trashResult;
    }

    /**
     * 音频信息组装
     * @param fileId
     * @return
     */
    private TrashResult getAudioRecord(Integer fileId) {
        CloudAudio cloudAudio = cloudAudioService.getById(fileId);
        String fileName = cloudAudio.getAudioName();

        //被封禁
        if (cloudAudio.getAudioBan().equals(1)) {
            fileName = fileName + "(已被封禁)";
        }

        TrashResult trashResult = TrashResult.builder()
                .fileId(cloudAudio.getId())
                .fileName(fileName)
                .fileImg(cloudAudio.getAudioImg())
                .fileDownload(cloudAudio.getAudioDownload())
                .fileSize(cloudAudio.getAudioSize())
                .build();

        return trashResult;
    }


    private TrashResult getVideoRecord(Integer fileId) {
        CloudVideo cloudVideo = cloudVideoService.getById(fileId);
        String fileName = cloudVideo.getVideoName();

        //被封禁
        if (cloudVideo.getVideoBan().equals(1)) {
            fileName = fileName + "(已被封禁)";
        }

        TrashResult trashResult = TrashResult.builder()
                .fileId(cloudVideo.getId())
                .fileName(fileName)
                .fileImg(cloudVideo.getVideoImg())
                .fileDownload(cloudVideo.getVideoDownload())
                .fileSize(cloudVideo.getVideoSize())
                .build();

        return trashResult;
    }

    @Override
    public RespEntity recoverTrash(TrashParam trashParam) {
        //恢复
        QueryWrapper<CloudTrash> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CloudTrash::getUserId, trashParam.getUserId())
                .eq(CloudTrash::getFileId, trashParam.getFileId())
                .eq(CloudTrash::getFileType, trashParam.getFileType())
                .eq(CloudTrash::getDeleted, 0);
        CloudTrash cloudTrash = baseMapper.selectOne(queryWrapper);
        cloudTrash.setDeleted(1);
        baseMapper.updateById(cloudTrash);

        //恢复文件数据
        switch (trashParam.getFileType()) {
            case 1 : recoverPicture(trashParam.getFileId()); break;
            case 2 : recoverDocument(trashParam.getFileId()); break;
            case 3 : recoverAudio(trashParam.getFileId()); break;
            case 4 : recoverVideo(trashParam.getFileId()); break;
        }

        return new RespEntity(RespCode.SUCCESS);
    }

    /**
     * 恢复图片
     * @param fileId
     */
    private void recoverPicture(Integer fileId) {
        CloudPicture cloudPicture = CloudPicture.builder()
                .id(fileId)
                .deleted(0)
                .build();
        cloudPictureService.updateById(cloudPicture);
    }

    /**
     * 恢复文档
     * @param fileId
     */
    private void recoverDocument(Integer fileId) {
        CloudDocument cloudDocument = CloudDocument.builder()
                .id(fileId)
                .deleted(0)
                .build();
        cloudDocumentService.updateById(cloudDocument);
    }

    /**
     * 恢复音频
     * @param fileId
     */
    private void recoverAudio(Integer fileId) {
        CloudAudio cloudAudio = CloudAudio.builder()
                .id(fileId)
                .deleted(0)
                .build();
        cloudAudioService.updateById(cloudAudio);
    }

    /**
     * 恢复视频
     * @param fileId
     */
    private void recoverVideo(Integer fileId) {
        CloudVideo cloudVideo = CloudVideo.builder()
                .id(fileId)
                .deleted(0)
                .build();
        cloudVideoService.updateById(cloudVideo);
    }

    @Override
    public RespEntity deleteTrash(TrashParam trashParam) {
        //彻底删除
        QueryWrapper<CloudTrash> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CloudTrash::getUserId, trashParam.getUserId())
                .eq(CloudTrash::getFileId, trashParam.getFileId())
                .eq(CloudTrash::getFileType, trashParam.getFileType())
                .eq(CloudTrash::getDeleted, 0);
        CloudTrash cloudTrash = baseMapper.selectOne(queryWrapper);
        cloudTrash.setDeleted(2);
        baseMapper.updateById(cloudTrash);

        //彻底删除数据
        switch (trashParam.getFileType()) {
            case 1 : deleteCompletePicture(trashParam.getFileId()); break;
            case 2 : deleteCompleteDocument(trashParam.getFileId()); break;
            case 3 : deleteCompleteAudio(trashParam.getFileId()); break;
            case 4 : deleteCompleteVideo(trashParam.getFileId()); break;
        }

        return new RespEntity(RespCode.SUCCESS);
    }

    /**
     * 彻底删除图片
     * @param fileId
     */
    private void deleteCompletePicture(Integer fileId) {
        CloudPicture cloudPicture = CloudPicture.builder()
                .id(fileId)
                .deleted(2)
                .build();
        cloudPictureService.updateById(cloudPicture);
    }

    /**
     * 彻底删除文档
     * @param fileId
     */
    private void deleteCompleteDocument(Integer fileId) {
        CloudDocument cloudDocument = CloudDocument.builder()
                .id(fileId)
                .deleted(2)
                .build();
        cloudDocumentService.updateById(cloudDocument);
    }

    /**
     * 彻底删除音频
     * @param fileId
     */
    private void deleteCompleteAudio(Integer fileId) {
        CloudAudio cloudAudio = CloudAudio.builder()
                .id(fileId)
                .deleted(2)
                .build();
        cloudAudioService.updateById(cloudAudio);
    }

    /**
     * 彻底删除视频
     * @param fileId
     */
    private void deleteCompleteVideo(Integer fileId) {
        CloudVideo cloudVideo = CloudVideo.builder()
                .id(fileId)
                .deleted(2)
                .build();
        cloudVideoService.updateById(cloudVideo);
    }
}
